import conf from '../conf'
import axios from 'axios'
import React, {Component} from 'react'

Component.prototype.$axios = axios

//axios
axios.defaults.withCredentials = true//表示跨域请求时是否需要使用凭证
axios.defaults.timeout = 5000 * 100;// 在超时前，所有请求都会等待 5 秒
axios.defaults.headers['Access-Control-Allow-Origin'] = conf.basicUrl;//
axios.defaults.withCredentials = true
// axios.defaults.baseURL = process.env.NODE_ENV === 'development' ? conf.basicUrl : ''


axios.interceptors.request.use(function (config) {
    return config;
}, function (error) {
    // 对请求错误做些什么
    return Promise.reject(error);
});


export const parseUrl = function parseUrl(url, parseObj = true) {
    let result = [];
    try {
        const query = url.split("?")[1];
        const queryArr = query.split("&");
        queryArr.forEach(function (item) {
            const obj = {};
            const key = item.split("=")[0];
            const value = item.split("=")[1];
            obj[key] = value;
            result.push(obj);
        });
        const temp = {};
        if (parseObj) {
            if (result.length > 0) {
                result.map((x, i) => {
                    Object.keys(x).forEach(key => {
                        temp[key] = x[key];
                    })
                })

            }
            result = temp;
        }
    } catch (e) {
    }
    return result;
}

React.Component.prototype.parseUrl = parseUrl;
